package com.newtouch.krd_app.http.websocket;

import static com.newtouch.krd_app.util.ConstUtil.ConnectionLostTimeout;
import static com.newtouch.krd_app.util.ConstUtil.HEART_BEAT_RATE;
import static com.newtouch.krd_app.util.ConstUtil.WidgetViewPdfDelayTime;
import static com.newtouch.krd_app.util.ConstUtil.WidgetViewPdfPeriodTime;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.hjq.toast.ToastUtils;
import com.newtouch.krd_app.http.api.TerminalApi;
import com.newtouch.krd_app.http.model.HttpData;
import com.newtouch.krd_app.util.BaseUrlUtil;
import com.newtouch.krd_app.util.ControllerInterface;

import org.java_websocket.enums.ReadyState;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Timer;
import java.util.TimerTask;

/**
 * author : TQ
 * time   : 2023/2/14
 * desc   : 长连接工具类，统一处理
 */
public class WebSocketUtil {
    private static final String TAG = "WebSocketUtil";
    private static WebSocketUtil mInstance;
    private Context mContext;
    public JWebSocketClient client;
    private String websocketurl = BaseUrlUtil.getInstance().getCurrentUrl().get("websocketUrl")+"/websocket/"+BaseUrlUtil.getInstance().getMac();
    private ControllerInterface controllerInterface;
    protected Timer timer;
    protected TimerTask timerTask;

    protected int anInt = 0;

    private WebSocketUtil(){

    }

    /**
     * 单列模式获取
     * @return
     */
    public static synchronized WebSocketUtil getInstance(){
        if (mInstance == null){
            mInstance = new WebSocketUtil();
        }
        return mInstance;
    }

    /**
     * 初始化长连接工具类
     * @param context
     * @param controllerInterface
     */
    public void initWebSocketUtil(Context context, ControllerInterface controllerInterface){
        this.controllerInterface = controllerInterface;
        this.mContext = context;
        if (client == null) {
            Log.e(TAG, "``````````````````````onResume");
            //    -------------------------------------websocket心跳检测------------------------------------------------
            if (timer == null) {
                timer = new Timer();
                timerTask = new TimerTask() {
                    @Override
                    public void run() {
                        if (client != null) {
                            if (client.isClosed()) {
                                Log.e("心跳包检测websocket连接状态：关闭", client.isOpen() + "/" + websocketurl);
                                reconnectWs();//心跳机制发现断开开启重连
                            } else {
                                Log.e("心跳包检测websocket连接状态：开启", client.isOpen() + "/" + websocketurl);
                                sendMsg("reconnect");
                            }
                        } else {
                            Log.e("心跳包检测websocket连接状态重新连接", "");
                            //如果client已为空，重新初始化连接
                            client = null;
                            initSocketClient();
                        }
//                        anInt ++;
//                        if (anInt == 20){
//                            try{
//                                String message  = "{\"msg\":\"操作成功\",\"code\":200,\"data\":{\"materialId\":410,\"terminalId\":459,\"fileId\":\"cdaef133d2b64b918be478cd9c2a88fc,fff666776e8e45beb8617d7bd29ebbb5,b4c2bfb422674466978ee1757e99e0af\",\"files\":[{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:04\",\"params\":{},\"fileId\":\"cdaef133d2b64b918be478cd9c2a88fc\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuATg4aAARFz-_FFUY527.pdf\",\"fileSize\":280015.00,\"delFlag\":\"0\",\"picFiles\":[{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:03\",\"remark\":\"1\",\"params\":{},\"fileId\":\"baa409c7740a4c5695ddae3c32f78a5f\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAK1YzAARebGzog0I552.jpg\",\"fileSize\":286316.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:04\",\"remark\":\"2\",\"params\":{},\"fileId\":\"f0329f5a19764ddfb38f658364d9be29\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAMdVVAAIv7aKv5Uk790.jpg\",\"fileSize\":143341.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:07\",\"remark\":\"1\",\"params\":{},\"fileId\":\"bc11c7276fe243ac896e3680117b6be7\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AIGVuAAQ8Uc7z-wg593.jpg\",\"fileSize\":277585.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"2\",\"params\":{},\"fileId\":\"549fecd2731d4ea59de6701fb66d3428\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AcOJ5AAYvx1F5h80464.jpg\",\"fileSize\":405447.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"3\",\"params\":{},\"fileId\":\"6a2ba2c6359c41e1bf55e83ab156e36e\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AaYImAAZVCYRTV8U108.jpg\",\"fileSize\":414985.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"4\",\"params\":{},\"fileId\":\"13fd49ca910d42129a9d56fc70bc7d7c\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AJYoFAADyFMjMfq8580.jpg\",\"fileSize\":61972.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"1\",\"params\":{},\"fileId\":\"4fcddceb62e9421a908f050c920a8790\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAQq0iAAUMODBm9G4125.jpg\",\"fileSize\":330808.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"2\",\"params\":{},\"fileId\":\"6aaee1ff9e7a47cabddb440c524fe19f\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAK85WAAUCfv98Tdk082.jpg\",\"fileSize\":328318.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"3\",\"params\":{},\"fileId\":\"c1eea6a2719a47bfafc2ed33525b2a3d\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSAdGU4AAVdyso8dxQ888.jpg\",\"fileSize\":351690.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:13\",\"remark\":\"4\",\"params\":{},\"fileId\":\"4168d80c5ae149cc8dd11e55b9114ca2\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSALciHAAUAeYROGkU876.jpg\",\"fileSize\":327801.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338}]},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"params\":{},\"fileId\":\"fff666776e8e45beb8617d7bd29ebbb5\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AJu0uAAvSF43-JNU690.pdf\",\"fileSize\":774679.00,\"delFlag\":\"0\",\"picFiles\":[{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:03\",\"remark\":\"1\",\"params\":{},\"fileId\":\"baa409c7740a4c5695ddae3c32f78a5f\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAK1YzAARebGzog0I552.jpg\",\"fileSize\":286316.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:04\",\"remark\":\"2\",\"params\":{},\"fileId\":\"f0329f5a19764ddfb38f658364d9be29\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAMdVVAAIv7aKv5Uk790.jpg\",\"fileSize\":143341.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:07\",\"remark\":\"1\",\"params\":{},\"fileId\":\"bc11c7276fe243ac896e3680117b6be7\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AIGVuAAQ8Uc7z-wg593.jpg\",\"fileSize\":277585.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"2\",\"params\":{},\"fileId\":\"549fecd2731d4ea59de6701fb66d3428\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AcOJ5AAYvx1F5h80464.jpg\",\"fileSize\":405447.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"3\",\"params\":{},\"fileId\":\"6a2ba2c6359c41e1bf55e83ab156e36e\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AaYImAAZVCYRTV8U108.jpg\",\"fileSize\":414985.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"4\",\"params\":{},\"fileId\":\"13fd49ca910d42129a9d56fc70bc7d7c\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AJYoFAADyFMjMfq8580.jpg\",\"fileSize\":61972.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"1\",\"params\":{},\"fileId\":\"4fcddceb62e9421a908f050c920a8790\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAQq0iAAUMODBm9G4125.jpg\",\"fileSize\":330808.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"2\",\"params\":{},\"fileId\":\"6aaee1ff9e7a47cabddb440c524fe19f\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAK85WAAUCfv98Tdk082.jpg\",\"fileSize\":328318.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"3\",\"params\":{},\"fileId\":\"c1eea6a2719a47bfafc2ed33525b2a3d\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSAdGU4AAVdyso8dxQ888.jpg\",\"fileSize\":351690.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:13\",\"remark\":\"4\",\"params\":{},\"fileId\":\"4168d80c5ae149cc8dd11e55b9114ca2\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSALciHAAUAeYROGkU876.jpg\",\"fileSize\":327801.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338}]},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:13\",\"params\":{},\"fileId\":\"b4c2bfb422674466978ee1757e99e0af\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSAdDwQAAgoe-Ts6JI790.pdf\",\"fileSize\":534651.00,\"delFlag\":\"0\",\"picFiles\":[{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:03\",\"remark\":\"1\",\"params\":{},\"fileId\":\"baa409c7740a4c5695ddae3c32f78a5f\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAK1YzAARebGzog0I552.jpg\",\"fileSize\":286316.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:04\",\"remark\":\"2\",\"params\":{},\"fileId\":\"f0329f5a19764ddfb38f658364d9be29\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkIuAMdVVAAIv7aKv5Uk790.jpg\",\"fileSize\":143341.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:07\",\"remark\":\"1\",\"params\":{},\"fileId\":\"bc11c7276fe243ac896e3680117b6be7\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AIGVuAAQ8Uc7z-wg593.jpg\",\"fileSize\":277585.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"2\",\"params\":{},\"fileId\":\"549fecd2731d4ea59de6701fb66d3428\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AcOJ5AAYvx1F5h80464.jpg\",\"fileSize\":405447.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"3\",\"params\":{},\"fileId\":\"6a2ba2c6359c41e1bf55e83ab156e36e\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AaYImAAZVCYRTV8U108.jpg\",\"fileSize\":414985.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:08\",\"remark\":\"4\",\"params\":{},\"fileId\":\"13fd49ca910d42129a9d56fc70bc7d7c\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkI-AJYoFAADyFMjMfq8580.jpg\",\"fileSize\":61972.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"1\",\"params\":{},\"fileId\":\"4fcddceb62e9421a908f050c920a8790\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_1.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAQq0iAAUMODBm9G4125.jpg\",\"fileSize\":330808.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"2\",\"params\":{},\"fileId\":\"6aaee1ff9e7a47cabddb440c524fe19f\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_2.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJOAK85WAAUCfv98Tdk082.jpg\",\"fileSize\":328318.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:12\",\"remark\":\"3\",\"params\":{},\"fileId\":\"c1eea6a2719a47bfafc2ed33525b2a3d\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_3.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSAdGU4AAVdyso8dxQ888.jpg\",\"fileSize\":351690.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338},{\"createBy\":\"softmedia\",\"createTime\":\"2023-03-10 10:06:13\",\"remark\":\"4\",\"params\":{},\"fileId\":\"4168d80c5ae149cc8dd11e55b9114ca2\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报_4.jpg\",\"fileType\":\"jpg\",\"filePath\":\"http://218.6.242.123:18000/group1/M00/00/1B/rBDYiWQKkJSALciHAAUAeYROGkU876.jpg\",\"fileSize\":327801.00,\"delFlag\":\"0\",\"height\":1653,\"width\":2338}]}],\"top\":[],\"range\":[],\"order\":[{\"materialId\":410,\"fileId\":\"b4c2bfb422674466978ee1757e99e0af\",\"onTop\":\"0\",\"playStartTime\":\"00:00:00\",\"playEndTime\":\"23:59:59\",\"fileName\":\"2023年3月9日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\"},{\"materialId\":410,\"fileId\":\"cdaef133d2b64b918be478cd9c2a88fc\",\"onTop\":\"0\",\"playStartTime\":\"00:00:00\",\"playEndTime\":\"23:59:59\",\"fileName\":\"2023年3月6日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\"},{\"materialId\":410,\"fileId\":\"fff666776e8e45beb8617d7bd29ebbb5\",\"onTop\":\"0\",\"playStartTime\":\"00:00:00\",\"playEndTime\":\"23:59:59\",\"fileName\":\"2023年3月8日作业现场安全督查情况通报.pdf\",\"fileType\":\"pdf\"}],\"cacheFileIds\":\"\"}}";
//                                HttpData<TerminalApi.Bean> result = GsonFactory.getSingletonGson().fromJson(message, HttpData.class);
//                                String str = new Gson().toJson(result.getData());
//                                TerminalApi.Bean data = new Gson().fromJson(str,TerminalApi.Bean.class);
//                                controllerInterface.dataUpdate(data);
//                            } catch (Exception e) {
//                                // 返回结果读取异常
//                                e.printStackTrace();
//                            }
//                        }
                    }
                };
            }
            //delay 4000不立即执行，等第一张显示4秒后执行
            //period 4000 间隔4秒执行循环
            timer.schedule(timerTask,HEART_BEAT_RATE,HEART_BEAT_RATE);
        } else if (!client.isOpen()) {
            reconnectWs();//进入页面发现断开开启重连
        }
    }

    /**
     * 重新初始化长连接，心跳当检查到client为null，会重新初始化
     */
    public void reInitWebSocket(){
        client = null;
    }


    /**
     * 初始化长连接
     */
    private void initSocketClient() {
//        if ()
        Log.e(TAG, "websocket的地址是：ws://" + websocketurl);
        URI uri = URI.create("ws://" + websocketurl);
        //TODO 创建websocket
        client = new JWebSocketClient(uri) {
            @Override
            public void onMessage(String message) {
                super.onMessage(message);

                if (message.contains("notice")){
                    controllerInterface.notice();
                }

                if (!message.equals("Heartbeat")&&message.contains("data")){
                    Log.e(TAG, "websocket收到消息：" + message);
                    try{
                        HttpData<TerminalApi.Bean> result = GsonFactory.getSingletonGson().fromJson(message, HttpData.class);
                        String str = new Gson().toJson(result.getData());
                        TerminalApi.Bean data = new Gson().fromJson(str,TerminalApi.Bean.class);
                        controllerInterface.dataUpdate(data);
                    } catch (Exception e) {
                        // 返回结果读取异常
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onOpen(ServerHandshake handshakedata) {
                super.onOpen(handshakedata);
                ToastUtils.show("网络连接成功");
                BaseUrlUtil.getInstance().isConnect = true;
                controllerInterface.playStart();
            }

            @Override
            public void onError(Exception ex) {
                super.onError(ex);
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                super.onClose(code, reason, remote);
                BaseUrlUtil.getInstance().isConnect = false;
                if (code!=1000) {
                    reconnectWs();//意外断开马上重连
                }
            }
        };
        //TODO 设置超时时间
        client.setConnectionLostTimeout(ConnectionLostTimeout);
        //TODO 连接websocket
        new Thread() {
            @Override
            public void run() {
                try {
                    //connectBlocking多出一个等待操作，会先连接再发送，否则未连接发送会报错
                    client.connectBlocking();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    /**
     * 发送消息
     *
     * @param msg
     */
    private void sendMsg(String msg) {
        if (null != client) {
            Log.e("", "^_^Websocket发送的消息：-----------------------------------^_^" + msg);
            if (client.isOpen()) {
                client.send(msg);
            }

        }
    }
    /**
     * 开启重连
     */
    private void reconnectWs() {
        if (client != null){
            new Thread() {
                @Override
                public void run() {
                    if (client.getReadyState().equals(ReadyState.NOT_YET_CONNECTED)){
                        try {
                            client.connectBlocking();
                        } catch (Exception e) {
                        }
                    }else if (client.getReadyState().equals(ReadyState.CLOSING) || client.getReadyState().equals(ReadyState.CLOSED)){
                        try {
                            client.reconnectBlocking();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }
    /**
     * 断开连接
     */
    private void closeConnect() {
        try {
            //关闭websocket
            if (null != client) {
                client.close();
            }
            if (timer !=null) {
                timer.cancel();
                timer = null;
            }
            if (timerTask !=null) timerTask.cancel();
            timerTask = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client = null;
        }
    }


}
